package cn.edu.jnu.basicms.service.impl;

import cn.edu.jnu.basicms.common.Page;
import cn.edu.jnu.basicms.entity.User;
import cn.edu.jnu.basicms.mapper.UserMapper;
import cn.edu.jnu.basicms.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource  // 注入Mapper
    private UserMapper userMapper;

    @Override
    public void insert(User user) {
        userMapper.insert(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    @Override
    public void deleteBatchUser(List<Integer> ids) {
        for (Integer id : ids) {
            userMapper.deleteUser(id);
        }
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> selectByName(String name) {
        return userMapper.selectByName(name);
    }

    @Override
    public List<User> selectByMultipleConditions(Map<String, Object> conditions) {
        return userMapper.selectByMultipleConditions(conditions);
    }

    @Override
    public Page<User> selectByPageAndName(String name, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<User> users = userMapper.selectByPageAndName(name, offset, pageSize);
        long total = userMapper.countByPageAndName(name);
        return new Page<>(users, total, pageSize, pageNum);
    }
}